package com.baili.dao.planner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.baili.dao.base.BaseDao;
import com.baili.dto.ProductOrder;
import com.baili.util.StringUtil;

/**
 * @author iceX
 * @version 创建时间：2016年3月31日 下午3:03:29
 * @Name: InspectorDaoImpl.java
 */
@Repository("PlannerDao")
public class PlannerDaoImpl extends BaseDao implements PlannerDao {

	@Override
	public List<Map<String, Object>> getProductOrderByRoundTypeP(
			ProductOrder productOrder) {

		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

		List<Map<String, Object>> pLst = this.sqlSessionTemplate.selectList(
				"planner.getProductOrderByRoundTypeP", productOrder);

		List<Map<String, Object>> psLst = this.sqlSessionTemplate.selectList(
				"planner.getProductOrderByRoundTypePScrap", productOrder);

		if (pLst != null && pLst.size() > 0) {

			lst.addAll(pLst);
		}

		if (psLst != null && psLst.size() > 0) {

			lst.addAll(psLst);
		}

		if (lst != null && lst.size() > 0) {

			for (Map<String, Object> map : lst) {

				map.put("processName", productOrder.getProcessName());
			}
		}

		return lst;
	}

	@Override
	public List getProductOrderByRoundTypeF(ProductOrder productOrder) {
		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

		List<Map<String, Object>> pLst = this.sqlSessionTemplate.selectList(
				"planner.getProductOrderByRoundTypeF", productOrder);

		List<Map<String, Object>> psLst = this.sqlSessionTemplate.selectList(
				"planner.getProductOrderByRoundTypeFScrap", productOrder);
		
		//TODO 此处调用一个方法，使用排程号与产品代码，查出所有round与roundType为0的信息，计数，如果数相等则不返回页面
		if (pLst != null && pLst.size() > 0) {

			lst.addAll(pLst);
		}

		if (psLst != null && psLst.size() > 0) {

			lst.addAll(psLst);
		}

		if (lst != null && lst.size() > 0) {

			for (Map<String, Object> map : lst) {
				
				String scheduleNo = map.get("scheduleNo").toString();
				String productCode = map.get("productCode").toString();
				
				productOrder.setScheduleNo(scheduleNo);
				
				productOrder.setProductCode(productCode);
				
//				List<Map<String, Object>> pssLst = this.sqlSessionTemplate.selectList(
//						"productorder.getProductOrderForFinishedForSize", productOrder);

				map.put("processName", productOrder.getProcessName());
			}
		}
		return lst;
	}

	@Override
	public List<Map<String, Object>> getProductOrderByRoundTypePSee(
			ProductOrder productOrder) {

		List<Map<String, Object>> lst = this.sqlSessionTemplate.selectList(
				"planner.getProductOrderByRoundTypePSee", productOrder);

		for (Map<String, Object> lMap : lst) {

			int notStartedCount = 0;

			int alreadyCompletedCount = 0;

			int outTimeCount = 0;

			productOrder.setProductCode(lMap.get("productCode").toString());
			
			productOrder.setScheduleNo(lMap.get("scheduleNo").toString());
			
			List<Map<String, Object>> pLst = this.sqlSessionTemplate
					.selectList("operater.getRoundListToSee", productOrder);

			for (Map<String, Object> map : pLst) {

				String state = map.get("state").toString();
				String kzType = map.get("kzType").toString();

				if (!state.equals("5")) {

					if (state.equals("1") && (kzType.equals("1") || kzType.equals("0"))) {

						notStartedCount = notStartedCount + 1;

						Date d_overTime = (Date) map.get("overTime");

						if ((new Date()).getTime() - d_overTime.getTime() > 0) {

							outTimeCount++;

						}

					} else {

						Date d_overTime = (Date) map.get("overTime");

						if ((new Date()).getTime() - d_overTime.getTime() > 0) {

							outTimeCount++;

						}
					}
				} else if (state.equals("5") && (kzType.equals("0") || kzType.equals("2"))) {

					alreadyCompletedCount = alreadyCompletedCount + 1;

				}
			}

			if (pLst.size() == notStartedCount) {

				lMap.put("status", 1);

			} else if (pLst.size() == alreadyCompletedCount) {

				lMap.put("status", 3);

			} else {
				lMap.put("status", 2);

			}

			if (outTimeCount > 0) {

				lMap.put("outTimeType", 1);

			} else {

				lMap.put("outTimeType", 0);

			}
		}

		return lst;
	}

	@Override
	public List<Map<String, Object>> getProductOrderByRoundTypeFSee(
			ProductOrder productOrder) {

		List<Map<String, Object>> fLst = this.sqlSessionTemplate.selectList(
				"planner.getProductOrderByRoundTypeFSee", productOrder);

		int notStartedCount = 0;

		int alreadyCompletedCount = 0;

		int outTimeCount = 0;

		for (Map<String, Object> map : fLst) {

			String state = map.get("state").toString();

			if (!state.equals("5")) {

				if (state.equals("1")) {

					notStartedCount = notStartedCount + 1;

					Date d_overTime = (Date) map.get("overTime");

					if ((new Date()).getTime() - d_overTime.getTime() > 0) {

						outTimeCount++;

					}

				} else {

					Date d_overTime = (Date) map.get("overTime");

					if ((new Date()).getTime() - d_overTime.getTime() > 0) {

						outTimeCount++;

					}
				}
			} else if (state.equals("5")) {

				alreadyCompletedCount = alreadyCompletedCount + 1;

			}
		}

		for (Map<String, Object> map : fLst) {

			if (fLst.size() == notStartedCount) {

				map.put("status", 1);

			} else if (fLst.size() == alreadyCompletedCount) {

				map.put("status", 3);

			} else {
				map.put("status", 2);

			}

			if (outTimeCount > 0) {

				map.put("outTimeType", 1);

			} else {

				map.put("outTimeType", 0);

			}
		}

		return fLst;
	}

	@Override
	public List getProductOrderByRoundTypePWarning(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"planner.getProductOrderByRoundTypePWarning", productOrder);
	}

	@Override
	public List getProductOrderByRoundTypeFWarning(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"planner.getProductOrderByRoundTypeFWarning", productOrder);
	}

	@Override
	public List handleProductOrderByProductCode(ProductOrder productOrder) {

		if (productOrder.getPlanner().equals("1")) {

			return this.sqlSessionTemplate.selectList(
					"planner.handleProductOrderByProductCodeP", productOrder);

		} else if (productOrder.getPlanner().equals("2")) {
			
			List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList(
					"planner.handleProductOrderByProductCodeF", productOrder);
			
			return lst;
		}

		return null;
	}
	
	
	@Override
	public List handleProductOrderByProductCodeSchedule(ProductOrder productOrder) {

		if (productOrder.getPlanner().equals("1")) {

			return this.sqlSessionTemplate.selectList(
					"planner.handleProductOrderByProductCodeP", productOrder);

		} else if (productOrder.getPlanner().equals("2")) {
			
			List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
			
			List<Map<String,Object>> roundList = this.sqlSessionTemplate.selectList("productorder.getMaxRoundF",productOrder);
			
			List<Map<String,Object>> FLst = this.sqlSessionTemplate.selectList(
					"planner.handleProductOrderByProductCodeF", productOrder);
			
			int buttonControl = 0;
			
			for(Map<String,Object> map : FLst){
				
				if(roundList != null && roundList.size() > 0){
					
					if(roundList.get(0) != null ){
						
						for(Map<String,Object> roundMap : roundList){
							
							if(roundMap != null){
								
								map.put("maxRound", roundMap.get("round"));
								
							}
						}
					}
					
				}
			}
					
			List<Map<String,Object>> scheduleLst = new ArrayList<Map<String,Object>>();
			
			List<Map<String,Object>> returnLst = new ArrayList<Map<String,Object>>();
			
			scheduleLst = this.sqlSessionTemplate.selectList(
							"planner.handleProductOrderByProductCodeSchedule", productOrder);
			
			List<Map<String,Object>>roundTypeRoundLst = this.sqlSessionTemplate.selectList("productorder.queryProductionOrderParameterFRoundTypeRound", productOrder);
			List<Map<String,Object>>rejectLst = this.sqlSessionTemplate.selectList("productorder.queryProductionOrderParameterFReject", productOrder);

			if(roundTypeRoundLst.size() > 0 || (rejectLst != null && rejectLst.size() > 0)){
				
				buttonControl = buttonControl + 1;
			}
			
			
			if(FLst != null && FLst.size() > 0){
				
				for(Map<String,Object> FMap : FLst){
					
					int count = 0;
					
					Map<String,Object> FNMap = new HashMap<String, Object>();
					
					if(scheduleLst != null && scheduleLst.size() > 0){
						
						for(Map<String,Object> scheduleMap : scheduleLst){
							
							if(FMap.get("size").toString().equals(scheduleMap.get("size").toString())){
								
								count = count + 1;
								
								returnLst.add(scheduleMap);
							}
						}
					}
					
					if(count == 0){
						
						for (Map.Entry<String, Object> entry : FMap.entrySet()) {
							 
							 FNMap.put(entry.getKey(), entry.getValue());
						}
						
						FNMap.put("productCount", 0);
						
						returnLst.add(FNMap);
					}
				}
			}
			
			if(FLst != null && FLst.size() > 0){
				
				for(Map<String,Object> FMap : FLst){
					
					if(buttonControl > 0){
						//为1时不能提交
						FMap.put("buttonControl", 1);
					}else{
						
						FMap.put("buttonControl", 0);
					}
				}
			}
			Map<String,Object> map1 = new HashMap<String, Object>();
			map1.put("pbsl", FLst);
			lst.add(map1);
			
			Map<String,Object> map2 = new HashMap<String, Object>();
			map2.put("ypsl", returnLst);
			lst.add(map2);
			
			return lst;
		}

		return null;
	}

	@Override
	public List getBomList(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList("planner.getBomList",
				productOrder);
	}

	@Override
	public List getNormedList(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList("planner.getNormedList",
				productOrder);
	}

	@Override
	public List<Map<String, Object>> warning() {

		return this.sqlSessionTemplate.selectList("planner.warning");
	}

	@Override
	public int getCount() {
		List<Object> list = this.sqlSessionTemplate
				.selectList("planner.getCount");
		int count = list.size();
		return count;
	}

	@Override
	public List<Map<String, Object>> getListByOvertimeType(
			ProductOrder productOrder) throws Exception {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

		List<Map<String, Object>> overtimeTypeLst = this.sqlSessionTemplate
				.selectList("planner.getListByOvertimeType", productOrder);
		
		int onTimeCount = 0;
		int outTimeCount = 0;
		int otherCount = 0;
		
		for(Map<String, Object> overtimeTypeMap : overtimeTypeLst){
			
			int productCount = Integer.parseInt(overtimeTypeMap.get("productCount").toString());
			
			Date overTime = sdf.parse(overtimeTypeMap.get("overTime").toString());
			
			Date realOverTime = sdf.parse(overtimeTypeMap.get("realOverTime").toString());
			
			int state = Integer.parseInt(overtimeTypeMap.get("state").toString());
			
			if(overTime.compareTo(realOverTime) > 0 && realOverTime.compareTo(sdf.parse("1970-01-01 08:00:00")) > 0 && state == 5){
				
				onTimeCount = onTimeCount + productCount;
		
			}else if(overTime.compareTo(realOverTime) < 0 || ((new Date()).compareTo(overTime) > 0 && realOverTime.compareTo(sdf.parse("1970-01-01 08:00:00")) == 0 && state != 5)){
				
				outTimeCount = outTimeCount + productCount;
				
			}else if(state > 0){
				
				otherCount = otherCount + productCount;
			}
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleCount", onTimeCount);
		map.put("state", 1);
		map.put("id", 1);

		lst.add(map);
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("roleCount", outTimeCount);
		map2.put("state", 2);
		map2.put("id", 2);
		
		lst.add(map2);
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("roleCount", otherCount);
		map3.put("state", 3);
		map3.put("id", 3);
		
		lst.add(map3);

		if(onTimeCount == 0 && outTimeCount == 0 && otherCount == 0){
			
			lst = null;
		}
		return lst;
	}

	@Override
	public List<Map<String, Object>> getListByOvertimeTypeP(
			ProductOrder productOrder) throws Exception {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

		List<Map<String, Object>> overtimeTypeLst = this.sqlSessionTemplate
				.selectList("planner.getListByOvertimeTypeP",productOrder);
		
		int onTimeCount = 0;
		int outTimeCount = 0;
		int otherCount = 0;
		
		for(Map<String, Object> overtimeTypeMap : overtimeTypeLst){
			
			int productCount = Integer.parseInt(overtimeTypeMap.get("productCount").toString());
			
			Date overTime = sdf.parse(overtimeTypeMap.get("overTime").toString());
			
			Date realOverTime = sdf.parse(overtimeTypeMap.get("realOverTime").toString());
			
			int state = Integer.parseInt(overtimeTypeMap.get("state").toString());
			
			if(overTime.compareTo(realOverTime) > 0 && realOverTime.compareTo(sdf.parse("1970-01-01 08:00:00")) > 0 && state == 5){
				
				onTimeCount = onTimeCount + productCount;
		
			}else if(overTime.compareTo(realOverTime) < 0 || ((new Date()).compareTo(overTime) > 0 && realOverTime.compareTo(sdf.parse("1970-01-01 08:00:00")) == 0 && state != 5)){
				
				outTimeCount = outTimeCount + productCount;
				
			}else if(state > 0){
				
				otherCount = otherCount + productCount;
			}
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleCount", onTimeCount);
		map.put("state", 1);
		map.put("id", 1);

		lst.add(map);
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("roleCount", outTimeCount);
		map2.put("state", 2);
		map2.put("id", 2);
		
		lst.add(map2);
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("roleCount", otherCount);
		map3.put("state", 3);
		map3.put("id", 3);
		
		lst.add(map3);
		
		if(onTimeCount == 0 && outTimeCount == 0 && otherCount == 0){
			
			lst = null;
		}

		return lst;
	}

	@Override
	public List<Map<String, Object>> getListByOvertimeTypeF(
			ProductOrder productOrder) throws Exception {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

		List<Map<String, Object>> overtimeTypeLst = this.sqlSessionTemplate
				.selectList("planner.getListByOvertimeTypeF",productOrder);
		
		int onTimeCount = 0;
		int outTimeCount = 0;
		int otherCount = 0;
		
		for(Map<String, Object> overtimeTypeMap : overtimeTypeLst){
			
			int productCount = Integer.parseInt(overtimeTypeMap.get("productCount").toString());
			
			Date overTime = sdf.parse(overtimeTypeMap.get("overTime").toString());
			
			Date realOverTime = sdf.parse(overtimeTypeMap.get("realOverTime").toString());
			
			int state = Integer.parseInt(overtimeTypeMap.get("state").toString());
			
			if(overTime.compareTo(realOverTime) > 0 && realOverTime.compareTo(sdf.parse("1970-01-01 08:00:00")) > 0 && state == 5){
				
				onTimeCount = onTimeCount + productCount;
		
			}else if(overTime.compareTo(realOverTime) < 0 || ((new Date()).compareTo(overTime) > 0 && realOverTime.compareTo(sdf.parse("1970-01-01 08:00:00")) == 0 && state != 5)){
				
				outTimeCount = outTimeCount + productCount;
				
			}else if(state > 0){
				
				otherCount = otherCount + productCount;
			}
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleCount", onTimeCount);
		map.put("state", 1);
		map.put("id", 1);

		lst.add(map);
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("roleCount", outTimeCount);
		map2.put("state", 2);
		map2.put("id", 2);
		
		lst.add(map2);
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("roleCount", otherCount);
		map3.put("state", 3);
		map3.put("id", 3);
		
		lst.add(map3);
		
		if(onTimeCount == 0 && outTimeCount == 0 && otherCount == 0){
			
			lst = null;
		}
		return lst;

	}

	@Override
	public Map<String, Object> getTimeTypeListByTime(String startTime,
			String overTime) {
		Map<String, Object> map = new HashMap<String, Object>();

		List<Object> onTimeList = this.sqlSessionTemplate
				.selectList("planner.getListByOvertimeTypeFOnTime");

		if (onTimeList != null && onTimeList.size() > 0) {

			map.put("onTime", Integer.parseInt(onTimeList.get(0).toString()));

		} else {

			map.put("onTime", 0);
		}

		List<Object> outTimeList = this.sqlSessionTemplate
				.selectList("planner.getTimeTypeListByTime");

		if (outTimeList != null && outTimeList.size() > 0) {

			map.put("outTimeList",
					Integer.parseInt(outTimeList.get(0).toString()));

		} else {

			map.put("outTimeList", 0);
		}

		return map;
	}

	/**
	 * 轮次制定(更新生产小组字段，更新轮次审核字段，以产品编码为条件对生产通知单表进行更新)
	 * 
	 * @param ProductOrder
	 * @return
	 */
	@Override
	public void updateProductOrderPTeam(ProductOrder productOrder) {

		this.sqlSessionTemplate.update("planner.updateProductOrderPTeam",
				productOrder);

	}

	@Override
	public List getApprovalList(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList("planner.getApprovalList",
				productOrder);
	}

	@Override
	public List getProductionOrderParam(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"planner.getProductionOrderParam", productOrder);
	}

	@Override
	public List getProcessName(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList("planner.getProcessName",
				productOrder);
	}

	@Override
	public List getProductTeam(ProductOrder productOrder) {

		List<Map<String, Object>> lst = this.sqlSessionTemplate.selectList(
				"planner.getProductTeam", productOrder);

		if (lst != null && lst.size() > 0) {

			for (Map<String, Object> map : lst) {

				String teamName = map.get("teamName").toString();
				String id = map.get("id").toString();

				Map usreMap = new HashMap();

				usreMap.put("id", id);

				List<Map<String, Object>> userList = this.sqlSessionTemplate
						.selectList("login.getUserByProductTeamId", usreMap);

				map.put("operator", userList);
			}
		} else {

			Map<String, Object> map = new HashMap<String, Object>();

			List<Map<String, Object>> userList = this.sqlSessionTemplate
					.selectList("login.getUserByProcessId", productOrder);
			map.put("operator", userList);

			lst.add(map);
		}

		return lst;
	}

	@Override
	public List getBLSize() {

		return this.sqlSessionTemplate.selectList("planner.getBLSize");
	}

	@Override
	public List getSizeBySex(Integer sex) {

		Map map = new HashMap();

		map.put("sex", sex);

		return this.sqlSessionTemplate.selectList("planner.getSizeBySex", map);
	}

	@Override
	public List getWarningDetails(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList("planner.getWarningDetails",
				productOrder);
	}
	@Override
	public List getWarehouseAndWorkShop() {
		
		List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		
		List<Map<String,Object>> warehouseLst = this.sqlSessionTemplate.selectList("planner.getWarehouse");
	
		map.put("warehouseLst", warehouseLst);
		
		List<Map<String,Object>> workShouLst = this.sqlSessionTemplate.selectList("planner.getWorkShop");
		
		map.put("workShouLst", workShouLst);
		
		lst.add(map);
		return lst;
	}
}
